﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HdipCricketClub.Models;

namespace HdipCricketClub.Controllers
{ 
    public class TeamController : Controller
    {
        private CricketClubEntities db = new CricketClubEntities();

        //
        // GET: /Team/List/

        public ViewResult List()
        {
            var teams = db.Teams.Include("Club").Include("Member");
            return View(teams.ToList());
        }

        //
        // GET: /Team/

        public ViewResult Index()
        {
            var teams = db.Teams.Include("Club").Include("Member");
            return View(teams.ToList());
        }

        //
        // GET: /Team/Details/5

        public ViewResult Details(int id)
        {
            Team team = db.Teams.Single(t => t.team_id == id);
            return View(team);
        }

        //
        // GET: /Team/Create

        public ActionResult Create()
        {            
            MemberRepository memberRepository = new MemberRepository();
            var players = memberRepository.FindAllPlayers();

            ViewBag.club_id = new SelectList(db.Clubs, "club_id", "name");
            ViewBag.captain = new SelectList(players, "member_id", "FirstAndLastName");
            return View();
        } 

        //
        // POST: /Team/Create

        [HttpPost]
        public ActionResult Create(Team team)
        {
            if (ModelState.IsValid)
            {
                db.Teams.AddObject(team);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            MemberRepository memberRepository = new MemberRepository();
            var players = memberRepository.FindAllPlayers();

            ViewBag.club_id = new SelectList(db.Clubs, "club_id", "name", team.club_id);
            ViewBag.captain = new SelectList(players, "member_id", "FirstandLastName", team.captain);
            return View(team);
        }
        
        //
        // GET: /Team/Edit/5
 
        public ActionResult Edit(int id)
        {
            Team team = db.Teams.Single(t => t.team_id == id);
            ViewBag.club_id = new SelectList(db.Clubs, "club_id", "name", team.club_id);

            MemberRepository memberRepository = new MemberRepository();
            var players = memberRepository.FindAllPlayers();
            ViewBag.captain = new SelectList(players, "member_id", "FirstAndLastName", team.captain);
            
            return View(team);
        }

        //
        // POST: /Team/Edit/5

        [HttpPost]
        public ActionResult Edit(Team team)
        {
            if (ModelState.IsValid)
            {
                db.Teams.Attach(team);
                db.ObjectStateManager.ChangeObjectState(team, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.club_id = new SelectList(db.Clubs, "club_id", "name", team.club_id);
            ViewBag.captain = new SelectList(db.Members, "member_id", "FirstAndLastName", team.captain);
            return View(team);
        }

        //
        // GET: /Team/Delete/5
 
        public ActionResult Delete(int id)
        {
            Team team = db.Teams.Single(t => t.team_id == id);
            return View(team);
        }

        //
        // POST: /Team/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            try
            {
                Team team = db.Teams.Single(t => t.team_id == id);
                db.Teams.DeleteObject(team);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            catch (Exception)
            {
                Team team = db.Teams.Single(t => t.team_id == id);
                return RedirectToAction("UpdateError", new { id = team.team_id });
            }

        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }

        //
        //  GET:    Team/UpdateError/

        public ActionResult UpdateError(int id)
        {
            Team team = db.Teams.Single(t => t.team_id == id);

            return View(team);
        }


    }
}